<template>
  <div>
    <el-dialog :title="formData.id?'编辑房产':'添加房产'" :visible="showDialog" @close="closeFn">
      <el-form ref="form" :model="formData" :rules="rules" label-width="80px">
        <el-form-item label="经济" prop="agent">
          <el-select v-model="formData.agent" placeholder="请选择经济">
            <el-option
              v-for="item in agents"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="分类" prop="category">
          <el-select v-model="formData.category" placeholder="请选择分类">
            <el-option
              v-for="item in categoryS"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="标题" label-width="80px" prop="title">
          <el-input v-model="formData.title" />
        </el-form-item>
        <el-form-item label="标签" prop="tags">
          <el-tag
            v-for="tag in dynamicTags"
            :key="tag"
            closable
            :disable-transitions="false"
            @close="handleClose(tag)"
          >
            {{ tag }}
          </el-tag>
          <el-input
            v-if="inputVisible"
            ref="saveTagInput"
            v-model="formData.tags"
            class="input-new-tag"
            size="small"
            @keyup.enter.native="handleInputConfirm"
            @blur="handleInputConfirm"
          />
          <el-button v-else class="button-new-tag" size="small" @click="showInput">+ New Tag</el-button>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-cascader
            v-model="value"
            :options="options"
            @change="handleChange"
          /><el-input v-model="data1" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="formData.price" />
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input v-model="formData.desc" type="textarea" />
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quillEditor v-model="formData.content" />
        </el-form-item>
        <!-- 封面 -->
        <el-form-item label="封面">
          <uploadImage ref="uploadImage1" />
        </el-form-item>
        <!-- 介绍图片 -->
        <!-- <el-form-item label="介绍图片">
          <uploadImage ref="uploadImage2" />
        </el-form-item> -->
        <el-form-item>
          <el-row type="flex" justify="end">
            <el-button type="primary" size="small" @click="confirmFn">确定</el-button>
            <el-button size="small" @click="closeFn">取消</el-button>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
const chinaData = require('@/components/address.json')
import { addHouse, getHouseDetails, updateHouse } from '@/api/house'
import { getAgents } from '@/api/agents'
import { getClassList } from '@/api/classify'
import { quillEditor } from 'vue-quill-editor' // 调用编辑器
import uploadImage from '@/components/uploadImage'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
export default {
  components: {
    quillEditor,
    uploadImage
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        title: '',
        price: null,
        address: null,
        desc: '', // 描述
        tags: '',
        image: ''

      },
      rules: {
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        desc: [{ required: true, message: '请输入内容', trigger: 'blur' }]
      },
      agents: [],
      categoryS: [],
      value: [],
      options: [],
      data1: null,
      dynamicTags: [],
      inputVisible: false,
      inputValue: ''

    }
  },
  computed: {
  },
  watch: {
  },
  async created() {
    // 获取经纪人列表
    const res = await getAgents()
    this.agents = res.data
    // 获取分类列表
    const res1 = await getClassList()
    this.categoryS = res1.data
    // 照片回显
    if (this.formData.image) {
      this.$refs.uploadImg1.fileList.push({
        url: this.formData.image
      })
    }
    // 地址级联
    this.options = chinaData.data.map(item => {
      return {
        value: item.province,
        label: item.province,
        children: item.city.map(val => {
          return {
            value: val.city,
            label: val.city,
            children: val.district.map(vle => {
              return {
                value: vle.district,
                label: vle.district
              }
            })
          }
        })
      }
    })
    // console.log(1212, this.options)
  },
  methods: {

    handleChange(value) {
      this.data1 = value.join(', ')
    },
    // 关闭弹窗
    closeFn() {
      this.$emit('update:showDialog', false)
      this.formData = {
        title: '',
        price: null,
        address: null,
        desc: '', // 描述
        tags: '',
        data1: null
      }
      this.$emit('uploadList')
      this.$refs.uploadImage1.fileList = []
      // 重置表单
      this.$refs.form.resetFields()
    },
    // 确认
    async confirmFn() {
      // 校验
      await this.$refs.form.validate()
      this.$nextTick(() => {
        this.formData.image = this.$refs.uploadImage1.fileList[0].url
      })
      if (this.formData.id) {
        await updateHouse(this.formData)
      } else {
        //   新增
        await addHouse(this.formData)
      }
      //   关闭弹窗
      this.closeFn()
    },
    // 获取房产详情
    async getHouseDetails(id) {
      const res = await getHouseDetails(id)
      this.formData = res.data
      // 根据id实现回显
      this.formData.agent = res.data.agent.id

      this.formData.category = res.data.category.id
      this.$nextTick(() => {
        this.$refs.uploadImage1.fileList = [{ url: this.formData.image }]
      })
    },
    handleClose(tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1)
    },

    showInput() {
      this.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    handleInputConfirm() {
      const inputValue = this.inputValue
      if (inputValue) {
        this.dynamicTags.push(inputValue)
      }
      this.inputVisible = false
      this.inputValue = ''
    }
  }
}
</script>
<style lang="scss">
.ql-container{
  height: 200px;
}
</style>
